package org.example;

public class Test{
  static {
    System.loadLibrary("test_jni");
  }

  private final long nativeApi; 
  private final long nativeObserver;

  public static interface Callback {
    public void onCall(String data);
  }

  public Test(Observer observer) {
    nativeObserver = nativeCreateCallback(observer);
    nativeApi = nativeCreate(nativeObserver);
    if (nativeApi == 0) {
      throw new RuntimeException("Failed to initialize Test!");
    }
  }
  
  public void dispose() {
    nativeFree(nativeApi);
    nativeApi = 0;
    nativeFreeCallback(nativeObserver);
    nativeObserver = 0;
  }

  public void setStr(String str){
    if (nativeApi == 0) {
      throw new RuntimeException("Test has disposed!");
    }
    nativeSetStr(nativeApi,str);
  }

  public String getStr(){
    if (nativeApi == 0) {
      throw new RuntimeException("Test has disposed!");
    }    
    return nativeGetStr(nativeApi);
  }

  public void getStr(){
    if (nativeApi == 0) {
      throw new RuntimeException("Test has disposed!");
    }     
    nativeTestCallback(nativeApi);
  } 

  private static native long nativeCreate(long nativeObserver);
  private static native void nativeFree(long nativeApi);

  private static native long nativeCreateCallback(Observer observer);
  private static native void nativeFreeCallback(long nativeObserver);

  private static native void nativeSetStr(long nativeApi,String str); 
  private static native String nativeGetStr(long nativeApi);
  private static native void nativeTestCallback(long nativeApi); 
}

